package com.liu.netty.util;

import com.liu.netty.pojo.Message;
import org.apache.commons.beanutils.BeanUtils;

import java.util.HashMap;
import java.util.Map;


public class MessageUtil {


    public static Message messageDecode(String bw) {


        Message message = new Message();
        Map map = new HashMap<String,String>();

        detailBwToMap(bw, map);

        try {
            BeanUtils.populate(message,map);
        }catch (Exception e){
            System.out.println("对象转换失败");
        }


        return message;
    }

    private static void detailBwToMap(String bw, Map map) {
//        System.out.println(bw);
        bw= bw.replace(">###" ,"");
        bw=  bw.replace("###<" ,"");

        String [] ss=  bw.split(";");
        for (String sss: ss) {
            if(sss.indexOf("{")>0){
                sss=sss.substring(sss.indexOf("{")+1);
            }
            if(sss.indexOf("}")>0){
                sss=sss.substring(0,sss.indexOf(">"));
            }
            if(sss.indexOf("<")>0){
                sss=sss.substring(sss.indexOf("<")+1);
            }
            if(sss.indexOf("=")>0){
                String [] aa=  sss.split("=");
                String key =lowerFirst(aa[0]);
//                System.out.println(key);

                map.put(key,aa[1]);
            }


        }
      //  System.out.println(map);

    }

    public static String lowerFirst(String oldStr){


        char[]chars = oldStr.toCharArray();

        chars[0] += 32;

        return String.valueOf(chars);

    }

    public static void main(String[] args) {
        String s= "###<TaskID=00188;TaskFlag=1;TaskContent{ObjectAddress=sx0047;CommandStyle=1;DataID=1;DataContent<Address=sx0047;SoftVersion=SV120190903;HardwareVersion=HV120190902>}>###";

//       s= "###<TaskID=00022;TaskFlag=1;TaskContent{ObjectAddress=sx0047;CommandStyle=1;DataID=2;DataContent<>}>###";
       Message message = messageDecode(s);
       System.out.println(message.getTaskID());

    }


}
